<?php

/**
 * 坤典智慧农场V6
 * @link https://www.cqkd.com
 * @description 软件开发团队为 重庆坤典科技有限公司
 * @description The software development team is Chongqing KunDian Technology Co., Ltd.
 * @description 软件著作权归 重庆坤典科技有限公司 所有 软著登记号: 2021SR0143549
 * @description 软件版权归 重庆坤典科技有限公司 所有
 * @description The software copyright belongs to Chongqing KunDian Technology Co., Ltd.
 * @description 本文件由重庆坤典科技授权予 重庆坤典科技 使用
 * @description This file is licensed to 重庆坤典科技-www.cqkd.com
 * @warning 这不是一个免费的软件，使用前请先获取正式商业授权
 * @warning This is not a free software, please get the license before use.
 * @warning 未经授权许可禁止转载分发，违者将追究其法律责任
 * @warning It is prohibited to reprint and distribute without authorization, and violators will be investigated for legal responsibility
 * @warning 未经授权许可禁止删除本段注释，违者将追究其法律责任
 * @warning It is prohibited to delete this comment without license, and violators will be held legally responsible
 */


namespace app\controller\admin\gateway;

use app\serve\ModbusRTUServices;
use app\serve\RedisServices;
use app\services\device\DeviceServices;
use app\services\gateway\GatewayLogServices;
use app\services\gateway\GatewayServices;
use app\services\label\LabelServices;
use app\validate\admin\GatewayValidate;
use app\validate\IdValidate;
use extend\RedisQueue;
use plugin\kundian\base\BaseController;


class GatewayController extends BaseController
{
    /**
     * 网关列表
     * @return \support\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGatewayList(){
        $where=getMore([
            ["gateway_status",''],
            ["marque_id",''],
            ["name",''],
        ]);
        $where["del"]=0;
        $where["admin_id"]=$this->adminId();
        $result=GatewayServices::getGatewayList($where);
        return success($result);
    }

    /**
     * 保存配置
     * @return \support\Response
     */
    public function saveGateway(){
        $param=$this->validate(GatewayValidate::class,"saveGateway");
        (new GatewayServices())->saveGateway($param,$this->adminId());
        return success();
    }

    /**
     * 保存配置
     * @return \support\Response
     */
    public function getGatewayInfo(){
        $param=$this->validate(IdValidate::class);
        $result=(new GatewayServices())->getOne(["id"=>$param['id'],"admin_id"=>$this->adminId()],"*",["marque","device"]);
        $result["label"]=(new LabelServices())->getLabelByIds($result["label_ids"]);
        return success($result);
    }


    /**
     * 保存配置
     * @return \support\Response
     */
    public function delGateway(){
        $param=$this->validate(IdValidate::class);
        $result=(new GatewayServices())->getOne(["id"=>$param['id'],"admin_id"=>$this->adminId()]);
        if (!$result){
            return  fail("网关不存在");
        }
        RedisServices::delGatewayRedis($result["code"]);
        (new GatewayServices())->update($param['id'],["del"=>1,'del_id'=>$this->adminId(),"del_time"=>date("Y-m-d H:i:s",time())]);
        return success();
    }

    /**
     * 返回mac值
     * @return \support\Response
     * @throws \Exception
     */
    public function getMac(){
        return success(["mac"=>get_order_sn("KD",12)]);
    }

    /**
     * 发送at命令
     * @return \support\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function sendAtInstruction(){
        $param=getMore([
            ["gateway_id",''],
            ["instruct_id",''],
            ["parameter",''],
        ]);
        (new GatewayServices())->sendAtInstruction($param['gateway_id'],$param["instruct_id"],$param["instruct_id"]);
        return success();
    }

    /**
     * 获取设备日志
     * @return \support\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGatewayLog(){
        $param=getMore([
            ["gateway_id",0],
            ["year",''],
            ["month",''],
            ["day",''],
        ]);
        $param["del"]=0;
        $param["admin_id"]=$this->adminId();
        $result=(new GatewayLogServices())->getList($param);
        return success($result);
    }

}
